#pragma once
#include<iostream>
#include<string>
#include<pthread.h>
#include<unistd.h>
#include<vector>

//要将线程封装，创建线程
//  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
//                           void *(*start_routine) (void *), void *arg);
 static uint32_t number = 1;
class Thread{
    void Ablerunning()
    {
        _isrunning=true;
    }
    void IsDetach()
    {
        _isdetach=true;
        std::cout<<"分离成功"<<std::endl;
    }
public:
Thread()
:_tid(0),
 _isdetach(0),
 _isrunning(0)
{
    _name="thread-"+std::to_string(number++);
}
//什么时候可以running,被create后
static void *Routine(void *arg)
{
Thread *self=(Thread *)arg;
self->Ablerunning();
std::cout<<self->_name<<std::endl;
return nullptr;

}

bool Start()
{
    if(_isrunning)
    return false;
    int n=pthread_create(&_tid,nullptr,Routine,this);
    if(n!=0)
    {
        std::cout<<"thread create fail"<<std::endl;
        return false;
    }
//_isrunning=1;
std::cout<<"create success"<<std::endl;
return true;


}
void Detach()
{
if(_isdetach)
return ;
pthread_detach(_tid);
IsDetach();
}
bool JoinAble()
{
    if(_isdetach)
    return false;
    int *a=nullptr;
   int n= pthread_join(_tid,(void**)&a);
   if(n!=0)
   {
    std::cout<<"join fail"<<std::endl;
    return false;
   }
   std::cout<<"join success"<<std::endl;
   return true;

}
~Thread(){}





    private:
pthread_t _tid;
std::string _name;
bool _isrunning;
bool _isdetach;

};